import axios from "axios";
import {ElMessage} from 'element-plus'
import router from'@/router/index.js'
const request = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
request.interceptors.request.use(
  (config) => {
    let res = localStorage.getItem('loginUser')
    if (res) {
      config.headers.token = JSON.parse(res).token
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    if(error.response.status==401){
      localStorage.removeItem('loginUser')
      ElMessage.error('登入超时,请重新登录')
      router.push('/login')
    }
    return Promise.reject(error);
  }
);
export default request;